<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- 整车保修期 -->
      <FormPanel ref="formPanel" :list="formList" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle"/>
      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
import { editSave,getTjdetail } from '@/api/claim/factory/FullCarWarrantyMaintain';
import {  getModelsdictt } from '@/api/dictionary/index';
import Vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import {dictionary} from'@/utils/dictMixin';
export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],
  data() {
    return {
      loading: false,
      visible: false,
      collapse: false,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList1(),
      saveData: null,
      modelNameList: []
    };
  },
  components: {
    Vehicle
  },
  async created() {
    await this.chuqi();
    await this.getDetailFun(this.rowData.vpId);
  },
  methods: {
     async chuqi(){
      const res = await getModelsdictt();
      if (res.resultCode == 200) {
        res.data.forEach(x => {
              if(x != null){
                this.modelNameList.push({text: x.MODEL_NAME,value: x.MODEL_CODE,id: x.MODEL_ID});
              }
            });
        this.formList.find(x => x.fieldName == 'modelCode').itemList = this.modelNameList;
      }
    },
    async getDetailFun(id) {
      this.loading = true;
      let res = (await getTjdetail({ id: id })).data;
      for (let i in res) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res[i];
          }
        });
      }
      this.ownerCode = res.ownerCode;
      this.appId = res.appId;
      this.ownerParCode = res.ownerParCode;
      this.isDeleted = res.isDeleted;
      this.createdBy = res.createdBy;
      this.modelName = res.modelName;
      this.createdAt = res.createdAt;
      this.loading = false;
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        for(let key in this.saveData) {
          this.rowData[key] = this.saveData[key];
        }
        this.rowData.createdBy=this.createdBy;
        this.rowData.createdAt=this.createdAt;
        this.rowData.isDeleted=this.isDeleted;
        this.rowData.appId = this.appId;
        this.rowData.updatedAt = null;
        this.rowData.updatedBy = null;
        this.modelNameList.forEach(x => {
          if(x.value === this.formList.find(x => x.fieldName == 'modelCode').initialValue){
            this.rowData.modelId = x.id;
            this.rowData.modelCode = x.value;
            this.rowData.modelName = x.text;
          }
        });
        this.loading = true;
        let res = await editSave(this.rowData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
    },
    createFormList1() {
      return [
        {
          type: 'SELECT',
          label: '适用车型',
          fieldName: 'modelCode',
          placeholder: '请选择适用车型...',
          initialValue: '',
          readonly: true,
          rules: [{ required: false, message: '请选择适用车型...', trigger: 'change' }],
          itemList: []
        },
        {
          type: 'SELECT',
          label: '营运类型',
          fieldName: 'vpType',
          placeholder: '请选择营运类型...',
          rules: [{required: false, message: '请选择营运类型...', trigger: 'change'}],
          initialValue: '',
          filterable: true,
          itemList: [
            { text: '运营', value: 55841001 },
            { text: '非运营', value: 55841002 }
          ]
        },
        {
          type: 'INPUT',
          label: '保修期',
          fieldName: 'vpPeriod',
          placeholder: '请输入保修期...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修期...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                callback();
              },
              trigger: ['blur', 'change'],
            },
          ],
          unitRender: () => {
            return <span>(月)</span>;
          },
          offsetRightCols: 1
        },
        {
          type: 'INPUT',
          label: '保修里程',
          fieldName: 'vpMile',
          placeholder: '请输入保修里程...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修里程...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                callback();
              },
              trigger: ['blur', 'change'],
            },
          ],
          unitRender: () => {
            return <span>(KM)</span>;
          },
          offsetRightCols: 1
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'vpStatus',
          filterable: true,
          placeholder: '请选择状态...',
          itemList: [{text: '启用', value: 1}, {text: '禁用', value: 0}],
          rules: [{required: true, message: '请选择状态...', trigger: 'change'}],
        }];
    }
  }
};
</script>

<style lang="less" scoped></style>
